package com.cw.controller;


import com.cw.common.api.ResultObject;
import com.cw.dto.BaseDictByIdAndNameRequest;
import com.cw.dto.BaseDictListDto;
import com.cw.dto.DeleteDTO;
import com.cw.entity.BaseDict;
import com.cw.mapper2.BaseDict2Mapper;
import com.cw.service.IBaseDictService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;

/**
* @author ly
* @since 2021-04-26
*/
@Api(tags = {"字典表"})
@RestController
@RequestMapping("/base-dict")
public class BaseDictController {

    @Autowired
    private IBaseDictService baseDictService;

    @Autowired
    private BaseDict2Mapper baseDict2Mapper;


    @GetMapping("getBaseDictById/{id}")
    @ApiOperation("通过id获得当前节点和子节点")
    public ResultObject getBaseDictById(@PathVariable String id) {
        return baseDictService.getBaseDictById(id);
    }

    @PostMapping("deleteBaseDictVoByIds")
    @ApiOperation(value = "删除字典树", notes = "ids:删除id数组")
    public ResultObject deleteBaseDictVoByIds(@RequestBody DeleteDTO deleteDTO) {
//        RequestAttributes ra = RequestContextHolder.getRequestAttributes();
//        ServletRequestAttributes sra = (ServletRequestAttributes) ra;
//        HttpServletRequest request = sra.getRequest();
//        String realName = Utf8ConverStringUtil.converString(request.getHeader("userRealName"));
//        String ip = getIpAddr(request);
//
//        //请求参数
//        Object paramObj = ids;
//        String param =
//                paramObj == null
//                        ? "无参数"
//                        : JSON.toJSONString(paramObj);
//        for(String id :ids){
//            StringBuilder stringBuilder = new StringBuilder();
//            String name = baseDictService.getById(id).getName();
//            SystemLog systemLog = new SystemLog();
//            systemLog.setAddress(ip);
//            systemLog.setParam(param);
//            stringBuilder.append("在");
//            stringBuilder.append("数字字典");
//            stringBuilder.append("中");
//            stringBuilder.append("删除了");
//            stringBuilder.append(name);
//            stringBuilder.append("。");
//            systemLog.setModule("数字字典");
//            systemLog.setOperating(stringBuilder.toString());
//            iSystemLogService.insertSystemLog(systemLog);
//        }
        return baseDictService.deleteBaseDictVoByIds(deleteDTO.getIds());
    }

    /**
     * 获取IP地址
     */
    private String getIpAddr(HttpServletRequest request) {
        String ip = request.getHeader("x-forwarded-for");
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("Proxy-Client-IP");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("WL-Proxy-Client-IP");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getRemoteAddr();
        }
        return "0:0:0:0:0:0:0:1".equals(ip) ? "127.0.0.1" : ip;
    }
//    @LogsAnnotation(typeCode = LogsConstant.insert,menuName ="数字字典")
    @PostMapping("addBaseDictVo")
    @ApiOperation(value = "添加字典树")
    public ResultObject addBaseDictVo(@RequestBody BaseDict baseDict) {
        return baseDictService.addBaseDictVo(baseDict);
    }

//    @LogsAnnotation(typeCode = LogsConstant.updated,menuName ="数字字典")
    @PostMapping("updateBaseDictVo")
    @ApiOperation(value = "更新字典树")
    public ResultObject updateBaseDictVo(@RequestBody BaseDict baseDict) {
        return baseDictService.updateBaseDictVo(baseDict);
    }

    @ApiOperation("字典表列表")
    @PostMapping("/getAllBaseDict")
    public ResultObject getAllBaseDict(@RequestBody BaseDictListDto baseDictListDto) {
        return ResultObject.successReturn(baseDictService.getAllBaseDicts(baseDictListDto));
    }

    @ApiOperation("批量删除")
    @DeleteMapping("/deleteBaseDictBatch")
    public ResultObject deleteBaseDictBatch(@RequestParam List<String> list) {
        return ResultObject.successReturn(baseDictService.deleteBaseDictBatch(list));
    }

    @ApiOperation("批量状态修改")
    @PutMapping("/updateBaseDictBatch")
    public ResultObject updateBaseDictBatch(@RequestBody List<String> list, String status) {
        return ResultObject.successReturn(baseDictService.updateEnableStatusBatch(list, status));
    }

    @ApiOperation("获取详情")
//    @LogsAnnotation(typeCode = LogsConstant.view,menuType = "数字字典")
    @GetMapping("/getBaseDict")
    public ResultObject getBaseDict(@RequestParam String id) {
        return ResultObject.successReturn(baseDictService.getBaseDictByIdDetail(id));
    }

    @PostMapping("/getBaseDictByIdAndName")
    @ApiOperation("数据项列表名称查询")
    public ResultObject getBaseDictByIdAndName(@RequestBody BaseDictByIdAndNameRequest baseDictByIdAndNameRequest) {
        return baseDictService.getBaseDictByIdAndName(baseDictByIdAndNameRequest.getId(),baseDictByIdAndNameRequest.getName(),baseDictByIdAndNameRequest.getStatus(),baseDictByIdAndNameRequest.getPageNum(),baseDictByIdAndNameRequest.getPageSize());
    }

    @ApiOperation("获取手工录入的下拉框数据")
    @GetMapping("/getBaseDictByIiPrepayin")
    public ResultObject getBaseDictByIiPrepayin() {
        return ResultObject.successReturn(baseDictService.getBaseDictByIiPrepayin());
    }

    @ApiOperation("获取诊断字典表")
    @GetMapping("/getDisgnosis")
    public ResultObject getDisgnosis(@RequestParam String name) {
        //入院诊断
        return ResultObject.successReturn(baseDict2Mapper.getDisgnosis(name));
    }

    @ApiOperation("获取医师列表-手工录入")
    @GetMapping("/getDoctList")
    public ResultObject getDoctList(@RequestParam String deptCode,String name) {
        return ResultObject.successReturn(baseDict2Mapper.getDoctList(name,deptCode));
    }

    @ApiOperation("通过病区编码获取病区下的床位")
    @GetMapping("/getBedByNurseCellCode")
    public ResultObject getBedByNurseCellCode(@RequestParam String nurseCellCode) {
        return ResultObject.successReturn(baseDict2Mapper.getBedByNurseCellCode(nurseCellCode));
    }
}

